/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.company.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.constant.Constants;
import com.jzwl.common.id.Sequence;
import com.jzwl.common.page.PageObject;
import com.jzwl.system.base.dao.BaseDAO;

@Repository("siteCompanyDao")
public class SiteCompanyDao {

	@Autowired
	private BaseDAO baseDAO;//dao基类，操作数据库
	@Autowired
	private Constants constants;

	public boolean addSiteCompany(Map<String, Object> map) {

		//自动注入时间戳为ID 酌情修改数据库类型为bigint  int会越界
		map.put("id", Sequence.nextId());

		String sql = "insert into " + constants.getCurrentDB() + "site_company "
				+ " (id,address,email,telephone,forecord) " + " values "
				+ " (:id,:address,:email,:telephone,:forecord)";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public String getColumns() {
		return "" + " id as id," + " address as address," + " email as email," + " telephone as telephone,"
				+ " forecord as forecord";
	}

	public PageObject querySiteCompanyList(Map<String, Object> map) {
		// [column]为字符串拼接, {column}为使用占位符. 如username='[username]',偷懒时可以使用字符串拼接 
		// [column] 为PageRequest的属性

		String sql = "select " + getColumns() + " from " + constants.getCurrentDB() + "site_company t where 1=1 ";

		if (null != map.get("address") && StringUtils.isNotEmpty(map.get("address").toString())) {
			sql = sql + " and t.address  = " + map.get("address") + "";
		}

		if (null != map.get("email") && StringUtils.isNotEmpty(map.get("email").toString())) {
			sql = sql + " and t.email  = " + map.get("email") + "";
		}

		if (null != map.get("telephone") && StringUtils.isNotEmpty(map.get("telephone").toString())) {
			sql = sql + " and t.telephone  = " + map.get("telephone") + "";
		}

		if (null != map.get("forecord") && StringUtils.isNotEmpty(map.get("forecord").toString())) {
			sql = sql + " and t.forecord  = " + map.get("forecord") + "";
		}

		sql = sql + " order by id ";

		PageObject po = baseDAO.queryForMPageList(sql, new Object[] {}, map);

		return po;
	}

	public boolean updateSiteCompany(Map<String, Object> map) {

		String sql = "update " + constants.getCurrentDB() + "site_company set ";
		if (null != map.get("address") && StringUtils.isNotEmpty(map.get("address").toString())) {
			sql = sql + "			address=:address ,";
		}
		if (null != map.get("email") && StringUtils.isNotEmpty(map.get("email").toString())) {
			sql = sql + "			email=:email ,";
		}
		if (null != map.get("telephone") && StringUtils.isNotEmpty(map.get("telephone").toString())) {
			sql = sql + "			telephone=:telephone ,";
		}
		if (null != map.get("forecord") && StringUtils.isNotEmpty(map.get("forecord").toString())) {
			sql = sql + "			forecord=:forecord ,";
		}
		sql = sql.substring(0, sql.length() - 1);
		sql = sql + " where id=:id";
		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean deleteSiteCompany(Map<String, Object> map) {

		String sql = "delete from " + constants.getCurrentDB() + "site_company where id in (" + map.get("id") + " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean delSiteCompany(Map<String, Object> map) {

		String sql = "update site_company set " + " isDelete =:" + map.get("isDelete") + " " + " where id=:id";

		return baseDAO.executeNamedCommand(sql, map);
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> getById(Map<String, Object> map) {

		String sql = "select " + getColumns() + " from " + constants.getCurrentDB() + "site_company where id = "
				+ map.get("id") + "";

		return baseDAO.queryForMap(sql);
	}

	public List<Map<String, Object>> querySelectValue(Map<String, Object> map) {

		String sql = " SELECT dd.id,dd.dic_name " + "    from dic_type  dt "
				+ "    LEFT JOIN  dic_data dd on dt.dic_id = dd.dic_id " + "    where dt.dic_code = '"
				+ map.get("code") + "'  and dd.isDelete = 0 ";
		return baseDAO.queryForList(sql);
	}

}
